package com.group4.DB;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.group4.DAO.RoleDAO;

public class Role {
	
	private int ID;
	private String roleName;
	private String roleDescription;

	static PreparedStatement ps = null;
	static ResultSet rs = null;
	
	public static RoleDAO getRoleWithID(int ID) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}
		
		RoleDAO role = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_role where ID = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				role = new RoleDAO();

				role.setID(ID);
				role.setRoleName(rs.getString("role_name"));
				role.setRoleDescription(rs.getString("role_description"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getRoleWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getRoleWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getRoleWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in getRoleWithID. ");
					e.printStackTrace();
				}
			}
		}
		return role;
	}

	public boolean addRole(RoleDAO Role) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		try {
			ps = ConnectionManager.conn
					.prepareStatement(" insert into tbl_Role (ID, role_name, role_description)"
							+ " values (?, ?, ?)");

			ps.setInt(1, Role.getID());
			ps.setString(2, Role.getRoleName());
			ps.setString(3, Role.getRoleDescription());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in addRole. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in addRole. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in addRole. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public boolean updateRole(RoleDAO Role) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			RoleDAO oldRole = getRoleWithID(Role.getID());

			if (oldRole == null) {
				System.out.println("No Role found with ID = " + Role.getID());
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("update tbl_Role set role_name = ?, role_description = ? where ID = ?");

			ps.setString(1, Role.getRoleName());
			ps.setString(2, Role.getRoleDescription());
			ps.setInt(3, Role.getID());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in updateRole. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in updateRole. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in addRole. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean deleteRole(int ID) {
		boolean result = false;
		
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		try {
			RoleDAO oldRole = getRoleWithID(ID);

			if (oldRole == null) {
				System.out.println("No Role found with ID = " + ID);
				return result;
			}
			
			ps = ConnectionManager.conn.prepareStatement("delete from tbl_role where ID = ?");

			ps.setInt(1, ID);

			ps.execute();
			
			result = true;

		} catch (SQLException e) {
			System.out.println("SQL Error in deleteRole. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in deleteRole. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in deleteRole. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();

		sb.append("Role [ID:").append(ID);
		sb.append("; role_name:").append(roleName);
		sb.append("; role_description:").append(roleDescription);
		sb.append("]");

		return sb.toString();
	}
}
